<#include "../public/header.html">
<link href="public/themes/talent.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="public/kindeditor/kindeditor.js"></script>
<script type="text/javascript" src="public/kindeditor/lang/zh_CN.js"></script>
<script type="text/javascript" src="public/js/ajaxUploadFile.js"></script>

    <div id="editdlg">
    </div>
        <div id="tb" style="padding:5px 5px; border-bottom: dashed #d2d2d2 1px;margin-bottom: 5px;">
            
        </div>
       <div id="Right"  style="height: 100%; width: 1236px;overflow:auto;">
            <div class="Item hr">
                <div class="current">素材管理</div>
                <div class="right">
                </div>
            </div>

            <input type="hidden" id="talent_id"/>
            <input type="hidden" id="msg_type" value="708">
            <div class="main_bd_more">
                <div class="main_bd_more_left">
                    <div id="more_content">

                    </div>
                    <a class="add_more_tab" onclick="add_detail();">
                        <li class="icon20_common add_gray">添加一条</li>
                    </a>
                </div>

                <div class="main_bd_one_right" style="height: 650px; ">
                    <input type="hidden" id="talent_detail_id"/>
                    <div class="content">
                        <div class="input_tab">
                            <div class="input_tab_title">
                                <span>标题</span>
                            </div>
                            <input type="text" class="input" id="title" oninput="title_on_change()" size="60"/>
                        </div>
                        <div class="input_tab">
                            <div class="input_tab_title">
                                <span>封面</span>
                            </div>
                            <input type="hidden" id="pic_url"/>
                            <input type="file" class="input" size="60" name="file_name" id="fileToUpload"/>
                            <input type="button" class="btn"  value="上传" onclick="ajaxFileUpload()"/>
                        </div>
                        <div class="input_tab">
                            <img src="" id="page_img" style="width: 40px;height: 40px;display: none"/>
                        </div>
                        <div class="input_tab">
                            <div class="input_tab_title">
                                <span>摘要</span>
                            </div>
                            <textarea rows="5" cols="60" id="description"></textarea>
                        </div>
                        <div class="input_tab">
                            <div class="input_tab_title">
                                <span>跳转URL</span>
                            </div>
                            <input type="text" class="input" id="url" size="60"/>
                        </div>
                        <div class="input_tab">
                            <div class="input_tab_title">
                                <span>正文</span>
                            </div>
                            <textarea style="height: 200px; width: 100%;"
                                      id="content"></textarea>
                        </div>

                        <div class="input_tab" style="text-align: center">
                            <br>
                            <button class="btn submit" onclick="add()">提交</button>
                            <button class="btn submit" id="delete_button" onclick="delete_detail()" >删除</button>
                        </div>

                    </div>

                </div>
            </div>
        </div>
		<script type="text/javascript">
			/*编辑器*/
	    	var content_edit;
	    	KindEditor.ready(function(K) {
	                content_edit = K.create('#content',{uploadJson : 'admin/File/upload'});
	        });
	        
	       $(function(){
	            $("#tb").prependTo(".datagrid-toolbar");
	            load();
	        })
	        /*标题*/
	        function title_on_change(){
	            var detail_id = $("#talent_detail_id").val();
	            if(detail_id == ""){
	                return;
	            }
	            var title = $("#title").val();
	            if(title == ""){
	                title="标题"
	            }
	            $("#view_title_id_"+detail_id).html(title);
	        }
	        /*增加一条*/
	        function add_detail(){
	        
	        	content_edit.sync();
	
	            var title = $("#title").val();
	            var pic_url = $("#pic_url").val();
	            var description = $("#description").val();
	            var url = $("#url").val();
	            var content = $("#content").val();
	            
	            $.ajax({
	                url:"admin/WXTalent/add_detail",
	                type:"post",
	                dataType:"json",
	                data:{talent_id:$("#talent_id").val(),talent_detail_id:$("#talent_detail_id").val(),title:title,pic_url:pic_url,description:description,url:url,content:content},
	                success:function(data){
	                    if(data.state==1){
	                        load();
	                    }
	                }
	            });
	        }
	        
	        /*增加*/
	        function add(){
	        
	        	content_edit.sync();
	
	            var title = $("#title").val();
	            var pic_url = $("#pic_url").val();
	            var description = $("#description").val();
	            var url = $("#url").val();
	            var content = $("#content").val();
	            
	            $.ajax({
	                url:"admin/WXTalent/add",
	                type:"post",
	                dataType:"json",
	                data:{talent_id:$("#talent_id").val(),talent_detail_id:$("#talent_detail_id").val(),title:title,pic_url:pic_url,description:description,url:url,content:content},
	                success:function(data){
	                    if(data.state==1){
	                        load();
	                    }
	                }
	            });
	        }
	        
	        /*加载数据*/
	        function load(){
	            $.ajax({
	                url:"admin/WXTalent/load",
	                type:"post",
	                dataType:"json",
	                data:{talent_id:$("#talent_id").val()},
	                success:function(data){
	                    if(data.state==1){
	                        var value = data.value;
	                        var str = "";
	                        for(var i=0;i<value.length;i++){
	                            var img_status = 'style="display:none;"';
	                            var li_status = '';
	                            var remove = 'remove="0"';
	                            if(i>1){
	                                remove = 'remove="1"';
	                            }
	                            if(value[i]["pic_url"] !=""&&value[i]["pic_url"] !=null){
	                                img_status = 'style="display:block;"'
	                                li_status = 'style="display:none;"'
	                            }
	
	                            if(i==0){
	                                str +='<div onclick="select(this)"  id="'+value[i]["id"]+'" '+remove+'>' +
	                                        '<div class="more_first " >' +
	                                        '<div class="h4_title">' +
	                                        '<div class="view_title" id="view_title_id_'+value[i]["id"]+'">'+value[i]["title"]+'</div>' +
	                                        '</div>' +
	                                        '<div class="first_img">' +
	                                        '<img src="/bk/'+value[i]["pic_url"]+'" id="view_img_id_'+value[i]["id"]+'"  class="first_img_img" '+img_status+'/>' +
	                                        '<li class="img_default" '+li_status+' id="img_default_id_'+value[i]["id"]+'" >封面图片</li>' +
	                                        '</div>' +
	                                      '</div></div>';
	                            }else{
	                                str +='<div  onclick="select(this)" class="more_second" id="'+value[i]["id"]+'"  '+remove+'>' +
	                                        '<div class="second_img">' +
	                                        '<img src="/bk/'+value[i]["pic_url"]+'" id="view_img_id_'+value[i]["id"]+'" class="second_img_img" '+img_status+'/>' +
	                                        '<li class="second_img_li" '+li_status+' id="img_default_id_'+value[i]["id"]+'" >缩略图</li>' +
	                                        '</div>' +
	                                        '<div class="second_title" id="view_title_id_'+value[i]["id"]+'">'+value[i]["title"]+'</div>' +
	                                      '</div>';
	                            }
	                        }
	
	                        $("#more_content").html(str);
	                        $("#title").val("");
				            $("#pic_url").val("");
				            $("#description").val("");
				            $("#url").val("");
				            $("#content").val("");
	                    }
	                }
	            });
	        }
	        
	        /*上传图片*/
	        function ajaxFileUpload() {
	            var detail_id = $("#talent_detail_id").val();
	            var fileToUpload = $("#fileToUpload").val();
	            if(fileToUpload == null || fileToUpload==""){
	                $.messager.alert("上传出错","请选择图片");
	                return;
	            }
	            if(detail_id == ""){
	                $.messager.alert("提醒","请在左边选择需要编缉的数据");
	                return;
	            }
	            $.ajaxFileUpload
	            (
	                    {
	                        url: 'admin/File/upload',
	                        secureuri: false,
	                        fileElementId: 'fileToUpload',
	                        dataType: 'json',
	                        success: function(result,status) {
	                        
	                            if(result.error == 0){
	
	                                $("#pic_url").val(result.url);
	                                $("#page_img").attr("src","/bk/"+result.url);
	                                $("#page_img").show();
	
	                                $("#view_img_id_"+detail_id).attr("src","/bk/"+result.url);
	                                $("#view_img_id_"+detail_id).show();
	                                $("#img_default_id_"+detail_id).hide();
	
	                            }
	
	                        },
	                        handleError:function(s, xml, status, e){
	
	                        }
	                    }
	            );
	            return false;
	        }
	        
	        
	        /*多图文 选择*/
	        function select(e){
	            $(".select").removeClass("select");
	            $(e).addClass("select");
	            $("#talent_detail_id").val($(e).attr("id"));
	            if($(e).attr("remove")==0){
	                $("#delete_button").hide();
	            }else{
	                $("#delete_button").show();
	            }
	
	            load_detail($(e).attr("id"));
	        }
		/*加载图文 详情*/
        function load_detail(id){
            $.ajax({
                url:"admin/WXTalent/load_detail",
                type:"post",
                dataType:"json",
                data:{talent_detail_id:id},
                success:function(data){
                    var value = data.value;
                    $("#title").val(value.title);
                    $("#pic_url").val(value.pic_url);

                    if(value.pic_url != null){
                        $("#page_img").attr("src",""+value.pic_url);
                        $("#page_img").show();
                    }else{
                        $("#page_img").hide();
                    }

                    $("#description").val(value.description);
                    $("#url").val(value.url);
                    content_edit.html(value.content==null?"":value.content);

                }
            });
        }
	        
	    </script>
    
    <#include "../public/footer.html">